<?php

namespace App\Http\Controllers;

use Config;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ListController extends Controller
{
    /**
     *前台订单列表页
     */   
    public function getIndex()
    {
        //检测用户有没有登录
        $id = session('id');
        if(!empty($id)){
            //查询订单表数据库orderid
            $orders = DB::table('orders')->where('userid',$id)->get();
        }else{
            return redirect('/log/log/log')->with('error','您还没有登录,请登录!');
        }
        // dd($order_goods);
        $lianjie = IndexController::listss();
        return view('comment.comment',['orders'=>$orders,'lianjie'=>$lianjie]);
    }

    /**
     * 订单详情
     */
    public function getXiang($orderid)
    {
        //查询购买商品表,获取number 根据goodid查询商品表
        $order_goods = [];
        $res = DB::table('order_goods')->where('orderid',$orderid)->get();
        foreach($res as $k=>$v){
            $tmp['orderid'] = $v->orderid;
            $tmp['goodid'] = $v->goodid;
            $tmp['number'] = $v->number;
            $tmp['unit_price'] = $v->unit_price;
            $goods = DB::table('goods')->where('id',$tmp['goodid'])->first();
            $tmp['goodname'] = $goods->goodname;
            $tmp['goodpic'] = $goods->goodpic;
            $order_goods[] = $tmp;
            $lianjie = IndexController::listss();
        }
        // var_dump($order_goods);
        //显示订单详情模板 分配变量
        return view('comment.listxiang',['order_goods'=>$order_goods,'lianjie'=>$lianjie]);
    }

    /**
     * 订单商品评论
     */
    public function getComment($data)
    {
        $res = explode('&', $data);
        $orderid = $res[0];
        $goodsid = $res[1];
        $lianjie = IndexController::listss();
        //显示模板,分配变量
        return view('comment.text',['orderid'=>$orderid,'goodsid'=>$goodsid,'lianjie'=>$lianjie]);
    }

    /**
     * 评论添加
     */
    public function postDocomment(Request $request)
    {
        $data= $request->except('_token');
        $data['userid'] = session('id');
        // dd($data);
        $res = DB::table('comment')->insert([$data]);
        // dd($res);
        //成功 重定向到详情列表页 
        if($res){
            return back()->with('success','评论成功');
        }
    }

    /**
     * 个人信息
     */
    public function getMessage()
    {
        //检测用户有没有登录
        $id = session('id');
        if(!empty($id)){
            //查询订单表数据库orderid
            $member = DB::table('member')->where('id',$id)->get();
            dd($member);
        }else{
            return redirect('/log/log/log')->with('error','您还没有登录,请登录!');
        }
        // dd($order_goods);
        $lianjie = IndexController::listss();
        return view('comment.comment',['orders'=>$orders,'lianjie'=>$lianjie]); 
    }
}
